package com.c1202m.gui;

import com.c1202m.model.ThongKeChuyenKhoanSearch;
import com.c1202m.util.MyTableFilter;
import java.awt.Color;
import javax.swing.JLabel;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableColumnModel;
import com.c1202m.model.ThongKeChuyenKhoanTableModel;
import com.c1202m.model.ThongKeGuiTienSearch;
import com.c1202m.model.ThongKeGuiTienTableModel;
import com.c1202m.model.ThongKeRutTienSearch;
import com.c1202m.model.ThongKeRutTienTableModel;
import com.c1202m.util.DateUtil;
import static org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ;
import org.jdesktop.beansbinding.BeanProperty;
import org.jdesktop.beansbinding.BindingGroup;
import org.jdesktop.beansbinding.Bindings;
import org.jdesktop.swingx.JXTableHeader;
import org.jdesktop.swingx.decorator.ColorHighlighter;
import org.jdesktop.swingx.decorator.HighlightPredicate;
import org.jdesktop.swingx.decorator.Highlighter;
import org.jdesktop.swingx.decorator.HighlighterFactory;

public class BangThongKe extends javax.swing.JPanel {

    ThongKeRutTienTableModel thongKeRutTienModel = new ThongKeRutTienTableModel();
//    ThongKeRutTienSearch thongKeRutTienSearch = new ThongKeRutTienSearch();
    ThongKeGuiTienTableModel thongKeGuiTienModel = new ThongKeGuiTienTableModel();
    ThongKeChuyenKhoanTableModel thongKeChuyenKhoanModel = new ThongKeChuyenKhoanTableModel();
    Highlighter simpleStripHL = HighlighterFactory.createSimpleStriping();
    public static String selectedSoTK;
    MyTableFilter filterController = null;

    public BangThongKe() {
        initComponents();
        new ThongKeRutTienSearch();
        new ThongKeGuiTienSearch();
        new ThongKeChuyenKhoanSearch();
        bangRutTien();
        bangGuiTien();
        bangChuyenKhoan();
    }

    private void bangRutTien() {
        jxTKRutTien.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxTKRutTien.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        TableColumnModel columnModel = jxTKRutTien.getColumnModel();
        jxTKRutTien.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxTKRutTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());

        filterController = new MyTableFilter(jxTKRutTien);
        BindingGroup filterGroup = new BindingGroup();
        filterGroup.addBinding(Bindings.createAutoBinding(READ,
                txtSearch, BeanProperty.create("text"),
                filterController, BeanProperty.create("filterString")));
        filterGroup.bind();
    }

    private void bangGuiTien() {
        jxTKGuiTien.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxTKGuiTien.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        TableColumnModel columnModel = jxTKGuiTien.getColumnModel();
        jxTKGuiTien.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxTKGuiTien.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
        filterController = new MyTableFilter(jxTKGuiTien);
        BindingGroup filterGroup = new BindingGroup();
        filterGroup.addBinding(Bindings.createAutoBinding(READ,
                txtSearch, BeanProperty.create("text"),
                filterController, BeanProperty.create("filterString")));
        filterGroup.bind();
    }

    private void bangChuyenKhoan() {
        jxTKChuyenKhoan.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        jxTKChuyenKhoan.addHighlighter(new ColorHighlighter(HighlightPredicate.ROLLOVER_ROW, null, new Color(255, 102, 0)));
        TableColumnModel columnModel = jxTKChuyenKhoan.getColumnModel();
        jxTKChuyenKhoan.setTableHeader(new JXTableHeader(columnModel) {
            @Override
            public void updateUI() {
                super.updateUI();
                if (getDefaultRenderer() instanceof JLabel) {
                    ((JLabel) getDefaultRenderer()).setHorizontalAlignment(JLabel.CENTER);
                }
            }
        });
        jxTKChuyenKhoan.getColumn(0).setCellRenderer(new MyTableRenderer.IDRenderer());
        filterController = new MyTableFilter(jxTKChuyenKhoan);
        BindingGroup filterGroup = new BindingGroup();
        filterGroup.addBinding(Bindings.createAutoBinding(READ,
                txtSearch, BeanProperty.create("text"),
                filterController, BeanProperty.create("filterString")));
        filterGroup.bind();
    }

    public static void lamMoi() {
        jxTKRutTien.setModel(new ThongKeRutTienTableModel());
        jxTKGuiTien.setModel(new ThongKeGuiTienTableModel());
        jxTKChuyenKhoan.setModel(new ThongKeChuyenKhoanTableModel());
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        txtSearch = new org.jdesktop.swingx.JXSearchField();
        jxDate = new org.jdesktop.swingx.JXDatePicker();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        jxTKRutTien = new org.jdesktop.swingx.JXTable();
        jScrollPane4 = new javax.swing.JScrollPane();
        jxTKGuiTien = new org.jdesktop.swingx.JXTable();
        jScrollPane5 = new javax.swing.JScrollPane();
        jxTKChuyenKhoan = new org.jdesktop.swingx.JXTable();

        setBackground(new java.awt.Color(255, 255, 255));
        setPreferredSize(new java.awt.Dimension(760, 450));

        jLabel1.setText("Tìm kiếm theo số tài khoản hoặc chủ tài khoản");

        jLabel2.setText("Tìm kiếm theo ngày");

        jLabel3.setText("Chọn ngày");

        txtSearch.setToolTipText("Nhập từ khóa tìm kiếm");
        txtSearch.setPreferredSize(new java.awt.Dimension(220, 20));
        txtSearch.setPrompt("Từ khóa tìm kiếm");

        jxDate.setPreferredSize(new java.awt.Dimension(170, 20));
        jxDate.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jxDateActionPerformed(evt);
            }
        });

        jxTKRutTien.setModel(thongKeRutTienModel);
        jxTKRutTien.setHighlighters(simpleStripHL);
        jxTKRutTien.setName("bangTKRutTien"); // NOI18N
        jxTKRutTien.setShowGrid(false);
        jScrollPane3.setViewportView(jxTKRutTien);

        jTabbedPane1.addTab("Rút Tiền", jScrollPane3);

        jxTKGuiTien.setModel(thongKeGuiTienModel);
        jxTKGuiTien.setHighlighters(simpleStripHL);
        jxTKGuiTien.setName("bangTKGuiTien"); // NOI18N
        jxTKGuiTien.setShowGrid(false);
        jScrollPane4.setViewportView(jxTKGuiTien);

        jTabbedPane1.addTab("Gửi Tiền", jScrollPane4);

        jxTKChuyenKhoan.setModel(thongKeChuyenKhoanModel);
        jxTKChuyenKhoan.setHighlighters(simpleStripHL);
        jxTKChuyenKhoan.setName("bangTKChuyenKhoan"); // NOI18N
        jxTKChuyenKhoan.setShowGrid(false);
        jScrollPane5.setViewportView(jxTKChuyenKhoan);

        jTabbedPane1.addTab("Chuyển Khoản", jScrollPane5);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 277, Short.MAX_VALUE)
                        .addComponent(jLabel3))
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jxDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(13, 13, 13))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 760, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtSearch, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(jxDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(410, Short.MAX_VALUE))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(65, 65, 65)
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 385, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jxDateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jxDateActionPerformed
        // TODO add your handling code here:
        if (jxDate.getDate() == null) {
            lamMoi();
            return;
        }
        jxTKRutTien.setModel(new ThongKeRutTienSearch());
        jxTKGuiTien.setModel(new ThongKeGuiTienSearch());
        jxTKChuyenKhoan.setModel(new ThongKeChuyenKhoanSearch());
    }//GEN-LAST:event_jxDateActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    public static org.jdesktop.swingx.JXDatePicker jxDate;
    public static org.jdesktop.swingx.JXTable jxTKChuyenKhoan;
    public static org.jdesktop.swingx.JXTable jxTKGuiTien;
    public static org.jdesktop.swingx.JXTable jxTKRutTien;
    private org.jdesktop.swingx.JXSearchField txtSearch;
    // End of variables declaration//GEN-END:variables
}
